# -*- coding: utf-8 -*-
import scrapy


class TaobaospiderItem(scrapy.Item):
    index = scrapy.Field()
    title = scrapy.Field()
    url = scrapy.Field()
    url_object_id = scrapy.Field()
    image_urls = scrapy.Field()
    # 图片本地保存路径,管道中手动处理
    image_files = scrapy.Field()
    push_date = scrapy.Field()
    market_price = scrapy.Field()
    taobao_price = scrapy.Field()

    def get_insert_sql_mysql(self):
        sql = """
            INSERT INTO jiaju(title,url,url_object_id,image_urls,image_files,push_date,market_price,taobao_price) 
            VALUES(%s,%s,%s,%s,%s,%s,%s,%s) 
            ON DUPLICATE KEY UPDATE title=VALUES(title)
        """
        if len(self["image_urls"]) == 1:
            self["image_urls"] = self["image_urls"][0]
        if len(self["image_files"]) ==1:
            self["image_files"] = self["image_files"][0]
        params = (
            self["title"],
            self["url"],
            self["url_object_id"],
            self["image_urls"],
            self["image_files"],
            self["push_date"],
            self["market_price"],
            self["taobao_price"],
        )
        return sql, params
